package com.example.unruly_zhao.mopaide.ui;


import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.unruly_zhao.mopaide.R;
import com.example.unruly_zhao.mopaide.global.Constants;
import com.example.unruly_zhao.mopaide.httpParams.Urls;
import com.example.unruly_zhao.mopaide.lock_screen.Lockscreen;
import com.example.unruly_zhao.mopaide.lock_screen.SharedPreferencesUtil;
import com.example.unruly_zhao.mopaide.mqtt.PushService;
import com.example.unruly_zhao.mopaide.util.LogUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;

import java.io.File;

import okhttp3.Call;
import okhttp3.Response;

public class MainActivity extends Activity {
    private SwitchCompat mSwitchd = null;
    private Context mContext = null;
    private String mDeviceID;
    private Button mButton;
    private Button mButton2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


      /*  getWindow().getDecorView().setSystemUiVisibility(

                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_IMMERSIVE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);*/

        mContext = this;
        setContentView(R.layout.activity_main);
        SharedPreferencesUtil.init(mContext);

        mDeviceID = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);

        mButton = (Button) findViewById(R.id.btn1);
        mButton2 = (Button) findViewById(R.id.btn2);

        initmButton();
        initmButton2();
    }

    private void initmButton2() {

        mButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                downVideo();

                SharedPreferencesUtil.setBoolean(Lockscreen.ISLOCK, false);

                Lockscreen.getInstance(mContext).stopLockscreenService();

                stopMqtt();

                onStopService();

            }
        });
    }

    private void stopMqtt(){

        PushService.actionStop(getApplicationContext());

        mButton.setEnabled(true);

        mButton2.setEnabled(false);

    }

    private void onStopService() {

        PushService.actionStop(getApplicationContext());

    }


    private void initmButton() {

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Lockscreen.getInstance(mContext).startLockscreenService();

                onStartService();

                startMqtt();

                finish();
            }
        });
    }
    private void startMqtt(){
        SharedPreferences.Editor editor = getSharedPreferences(PushService.TAG, MODE_PRIVATE).edit();

        editor.putString(PushService.PREF_DEVICE_ID, mDeviceID);

        editor.commit();

        PushService.actionStart(getApplicationContext());

        mButton.setEnabled(false);
        mButton2.setEnabled(true);
    }

    private void onStartService(){

        PushService.actionStart(getApplicationContext());

    }


    /**
     * 下载视频
     */

    private void downVideo() {

        String videoFilePath = Constants.FILE_PATH + "video.mp4";
        File file = new File(videoFilePath);
        if (!file.exists()) {
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                down();

            } else {
                Toast.makeText(this, "sd卡未挂载", Toast.LENGTH_LONG).show();
            }
        } else {
            Toast.makeText(this, "已存在此文件", Toast.LENGTH_LONG).show();
        }
    }

    private void down() {

        String downPath = Constants.FILE_PATH;
        //  String downPath = Environment.getExternalStorageDirectory() + "";

        LogUtil.e("down", "downPath:" + downPath);

        File file = new File(downPath);
        if (!file.exists()) {
            LogUtil.e("down", "mkdir");
            file.mkdir();
        }

        if (Build.VERSION.SDK_INT > 23) {
            /**Android 7.0以上的方式**/
            LogUtil.e("down", "Build.VERSION.SDK_INT > 23");
        }

        OkGo.get(Urls.VIDEO_URL)
                .execute(new FileCallback(downPath, "video.mp4") {

                    @Override
                    public void onSuccess(File file, Call call, Response response) {
                        LogUtil.e("down", "down");
                    }

                    @Override
                    public void downloadProgress(long currentSize, long totalSize, float progress, long networkSpeed) {

                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        LogUtil.e("down", e.toString());

                    }
                });

    }


    @Override
    protected void onResume() {
        super.onResume();
       /* getWindow().getDecorView().setSystemUiVisibility(

                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_IMMERSIVE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);*/

        SharedPreferences p = getSharedPreferences(PushService.TAG, MODE_PRIVATE);
        boolean started = p.getBoolean(PushService.PREF_STARTED, false);

        ((Button) findViewById(R.id.btn1)).setEnabled(!started);
        ((Button) findViewById(R.id.btn2)).setEnabled(started);

        /**
         * 设置为横屏
         */
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        super.onResume();
    }



}
